package com.softqin.washer.db;


import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.softqin.washer.bean.AddressBean;

public class WashDbManager implements WashDbManagerInterface{

	private Context context;
	public static WashDbManager instance;
	private WashDbHelper helper;
	
	public synchronized static WashDbManager getInstance(Context context) {
		if (instance == null) {
			instance = new WashDbManager(context);
		}
		return instance;
	}
	
	/**
	 * 初始化的时候需要父类把context传进去
	 * @param context
	 */
	public WashDbManager(Context context)
	{
		this.context=context;
	}
	
	@Override
	public void createDatabase() {
		// 创建数据库
		helper=new WashDbHelper(context, WashDB.DB_NAME, null, WashDB.DB_VERSION);
		helper.getReadableDatabase();
	}


	@Override
	public long addAddress(AddressBean addressBean) {
		long result = -1l;
		helper=new WashDbHelper(context, WashDB.DB_NAME, null, WashDB.DB_VERSION);
		ContentValues contentValues = new ContentValues();
		contentValues.put(WashDB.ADDRESS_TB.TOKEN_ID, addressBean.getTokenid());
		contentValues.put(WashDB.ADDRESS_TB.DRIVER_ID, addressBean.getdTokenid());
		contentValues.put(WashDB.ADDRESS_TB.ADDRESS, addressBean.getAddress());
		result = helper.getWritableDatabase().insert(WashDB.ADDRESS_TB.TABLE_NAME, null, contentValues);
		helper.close();
		return result;
	}

	@Override
	public List<AddressBean> findAllAddress() {
		List<AddressBean> lisAddressBeans = new ArrayList<AddressBean>();
		helper=new WashDbHelper(context, WashDB.DB_NAME, null, WashDB.DB_VERSION);
		Cursor cursor = helper.getWritableDatabase().rawQuery("select * from " + WashDB.ADDRESS_TB.TABLE_NAME, null);
		while (cursor.moveToNext()) {
			AddressBean addressBean = new AddressBean();
			addressBean.setTokenid(cursor.getString(cursor.getColumnIndex(WashDB.ADDRESS_TB.TOKEN_ID)));
			addressBean.setAddress(cursor.getString(cursor.getColumnIndex(WashDB.ADDRESS_TB.ADDRESS)));
			addressBean.setdTokenid(cursor.getString(cursor.getColumnIndex(WashDB.ADDRESS_TB.DRIVER_ID)));
			lisAddressBeans.add(addressBean);
		}
		return lisAddressBeans;
	}

	

}
